package xx.pro.future;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<Integer> future = executor.submit(() -> {
            // 模拟耗时计算
            Thread.sleep(2000);
            return 2 + 3;
        });
        System.out.println("异步计算中...");

        System.out.println("判断任务是否已结束的方法" + future.isDone());

        System.out.println("判断任务是否已取消的方法" + future.isCancelled());

        // 阻塞等待计算结果
        int result = future.get();

        System.out.println("计算结果: " + result);
        executor.shutdown();
    }
}
